/////////////////////////////////////////////////////////////////////////
// MIT License
//
// Copyright (c) 2000-2011 OpenNETCF Consulting, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  
////////////////////////////////////////////////////////////////////////

using System;
using System.Collections;

namespace OpenNETCF.Windows.Forms.Calendar
{
	/// <summary>
	/// Represents a collection of calendar items
	/// </summary>
	public class CalendarItemCollection: ICollection, IList
	{
		private ArrayList m_alItems;
        internal ItemListChangedDelegate ItemListChanged;

        /// <summary>
        /// Default contructor
        /// </summary>
		public CalendarItemCollection()
		{
			m_alItems = new ArrayList();
		}

        /// <summary>
        /// Copies the collection to an array of CalendarItem
        /// </summary>
        /// <returns></returns>
        public CalendarItem[] ToArray()
        {
            return (CalendarItem[])m_alItems.ToArray(typeof(CalendarItem));
        }

        /// <summary>
        /// Sorts the collection
        /// </summary>
		public void Sort()
		{
		}

        private void FireOnListChanged()
        {
            if ( ItemListChanged != null )
                ItemListChanged();
        }

		
		#region ICollection Members
        /// <summary>
        /// Gets a value indicating whether access to the collection is syncronized
        /// </summary>
		public bool IsSynchronized
		{
			get
			{
				return m_alItems.IsSynchronized;
			}
		}

        /// <summary>
        /// Gets the number of elements actually contained in the collection
        /// </summary>
		public int Count
		{
			get
			{
				return m_alItems.Count;
			}
		}
        /// <summary>
        /// Copies an array of elements from one array to the other specified by the start index
        /// </summary>
        /// <param name="array"></param>
        /// <param name="index"></param>
		public void CopyTo(Array array, int index)
		{
			m_alItems.CopyTo(array, index);
		}

        /// <summary>
        /// Gets an object that can be used to synchronize access to the collection
        /// </summary>
		public object SyncRoot
		{
			get
			{
				return m_alItems.SyncRoot;
			}
		}

		#endregion

		#region IEnumerable Members

        /// <summary>
        /// Returns an enumerator for the collection
        /// </summary>
        /// <returns></returns>
		public IEnumerator GetEnumerator()
		{
			return m_alItems.GetEnumerator();
		}

		#endregion

		#region IList Members

        /// <summary>
        /// Gets a value indicating if the collection is readonly
        /// </summary>
		public bool IsReadOnly
		{
			get
			{
				return false;
			}
		}

		object IList.this[int index]
		{
			get
			{
				return m_alItems[index];
			}
			set
			{
				if ( !(value is CalendarItem) )
					throw new ArgumentException();
				m_alItems[index] = value;
                FireOnListChanged();
			}
		}

        /// <summary>
        /// Gets the calendar item by index
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
		public CalendarItem this[int index]
		{
			get
			{
				return m_alItems[index] as CalendarItem;
			}
		}

        /// <summary>
        /// Removes an element from the collection at the specified index
        /// </summary>
        /// <param name="index"></param>
		public void RemoveAt(int index)
		{
			m_alItems.RemoveAt(index);
             FireOnListChanged();
		}

		void IList.Insert(int index, object value)
		{
			m_alItems.Insert(index, value);
            FireOnListChanged();
		}

        /// <summary>
        /// Removes the first occurrence of the specified object
        /// </summary>
        /// <param name="value"></param>
		public void Remove(object value)
		{
			m_alItems.Remove(value);
            FireOnListChanged();
		}

		bool IList.Contains(object value)
		{
			return m_alItems.Contains(value);
		}

        /// <summary>
        /// Determins whether an element is in the collection
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
		public bool Contains(CalendarItem value)
		{
			return m_alItems.Contains(value);
		}

        /// <summary>
        /// Removes all elements from the collection
        /// </summary>
		public void Clear()
		{
			m_alItems.Clear();
             FireOnListChanged();
		}

		int IList.IndexOf(object value)
		{
			return m_alItems.IndexOf(value);
		}

        /// <summary>
        /// Searches for the CalenderItem and returns the index within the collection
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
		public int IndexOf(CalendarItem value)
		{
			return m_alItems.IndexOf(value);
		}

		int IList.Add(object value)
		{
			return m_alItems.Add(value);
		}

        /// <summary>
        /// Adds a CalendarItem to the end of the collection
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
		public int Add(CalendarItem value)
		{
			m_alItems.Add(value);
			m_alItems.Sort(new CalendarItemComparer());
            FireOnListChanged();
            return m_alItems.IndexOf(value);
		}

        /// <summary>
        /// Gets a value indicating if the collection is a fixed size
        /// </summary>
		public bool IsFixedSize
		{
			get
			{
				return m_alItems.IsFixedSize;
			}
		}

		#endregion
	}

}
